package demo2.controller;

import demo2.pojo.Point24;
import demo2.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * ，
 * 对game对象list数据，
 * game.solve
 * <p>
 * 更新视图
 * 把game.getSolution 传出到视图 status
 */
@Controller
@RequestMapping("")
public class SolveGameController {
    private Game game = Game.getInstance();

    @RequestMapping("solve")
    ModelAndView solveGame() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("a", game.getDigitAt(0));
        mav.addObject("b", game.getDigitAt(1));
        mav.addObject("c", game.getDigitAt(2));
        mav.addObject("d", game.getDigitAt(3));
        GameHandler[] chains =new GameHandler[]{
                //  new QueryDBGameHandler(),
                new RandomGameHandler(),

                new BruteGameHandler(),//32w+
                new AIGameHandler(),//7k+


        };

        //   mav.addObject("status", "solve"+game.getDigits().toString());
        for (int i = 0; i < chains.length - 1; i++)
            chains[i].setNextHandler(chains[i+1]);
        chains[0].handleRequest();


        List<Integer> list = game.getDigits().subList(0, 4);
        Collections.sort(list);
        List<Point24> result = new ArrayList<>();
        for (String sol : game.getSolution()) {
            Point24 point24 = new Point24();
            point24.setNumbers(list.toString());
            point24.setSolution(sol);
            result.add(point24);
        }

        mav.addObject("status", "AI computed " + result.size() + " solution(s).");

        mav.addObject("cs", result);
        mav.setViewName("index");
        return mav;


    }
}